package com.example.annatation;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.Field;

public class TestFilter implements Filter {

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        try {
            HttpServletRequest request = (HttpServletRequest) req;
            String uri = request.getRequestURI();
            Class<?> clazz = TestEnum.getClass(uri);
            Object obj = clazz.newInstance();
            TestAnnotation testAnnotation = clazz.getAnnotation(TestAnnotation.class);
            if (testAnnotation != null) {  //类上存在TestAnnotation注解，处理全局变量
                Field field = clazz.getDeclaredField("name");
                field.setAccessible(true);
                String s = (String) field.get(obj);
                System.out.println(s + "+++pppppppppppppppppppp");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        chain.doFilter(req, resp);
    }
}
